#define NO_OF_ALPHABETES	7

struct alphabet {
    int pid;
    char** actions;
    int lazyDeleted;

};

struct alphabet *alphabetList[NO_OF_ALPHABETES];

int hashIndex(int PID){
	return (PID%NO_OF_ALPHABETES);
}

void insertAlphabet(pid, alphabet, actions)
struct alphabet *alphabet;
int pid;
char **actions;
{

	int arrayIndex, hashedArrayIndex;
	int i;
	hashedArrayIndex = hashIndex(pid);
	printf("inserting alphabet with PID: %d and arrayindexe:%d \n",pid,hashedArrayIndex);
	arrayIndex = find_index_by_linear_prob(alphabet, -1);
	alphabet[arrayIndex].pid=pid;
	alphabet[arrayIndex].actions=actions;
	/*printf("Test: %s\n",alphabet[arrayIndex].actions[2]);

	for(i = 0; alphabet[arrayIndex].actions[i] != '\0'; i++)
	{
	   printf("%s", alphabet[arrayIndex].actions[i]);
	}*/

}


int find_index_by_linear_prob(alphabet, PID)
struct alphabet * alphabet;
int PID;
{
    int i, hashedIndex, initArrayIndex;
    initArrayIndex= hashIndex(PID);
    if (PID == -1) {
        /*Find an empty slot*/
        for (i = 0; i < NO_OF_ALPHABETES; i++) {
            /*If p_nr =-1 then the arrayslot has never been used, so it's empty.
             *If the lazyDeletedflag is 1 then then arrayslot can be seen as empty so it can be used again.
             */
            hashedIndex = hashIndex(i + initArrayIndex);
            if (alphabet[hashedIndex].pid == -1 || alphabet[hashedIndex].lazyDeleted == 1) {
                return hashedIndex;
            }

        }
    } else {
        /*Find the arrayindex of a proc*/
        for (i = 0; i < NO_OF_ALPHABETES; i++) {
            /*If p_nr =-1 then the arrayslot has never been used, so it's empty.
             *If the lazyDeletedflag is 1 then then arrayslot can be seen as empty so it can be used again.
             */
            hashedIndex = hashIndex(i + initArrayIndex);
            if (alphabet[hashedIndex].pid == PID) {
                return hashedIndex;
            }

        }
    }
    /*If arrayindex can be found the array is full or the proc does not exist in the table*/
    return -1;
}
